﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SuperRyoninRPG.Characters.Items;
using SuperRyoninRPG.Display;

namespace SuperRyoninRPG.Main.Tiles
{
	class ItemBlock<TItem> : Tile where TItem : Item
	{
		private Item item;

		private const float bumpMaxTime = 0.05f;

		private float bumpTime;
		private float bumpVelocity = 300.0f;
		private float bumpPosition;

		public ItemBlock(string imagesPath, TileCollision collision, Stage stage, int x, int y, int frameWidth,TItem item)
			: base(imagesPath, collision, stage, x, y, frameWidth)
		{
			var itemPosition = new Vector2(x * Width, (y + 1) * Height);
			this.item = item;
		}

		public Type ItemType { get; set; }

		public override void Bumped()
		{
			if (this.State == State.Bumped)
			{
				return;
			}
			this.bumpTime = bumpMaxTime;
			this.Stage.AddItem(this.item);
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			if (this.State == State.Bumped)
			{
				return;
			}

			if (bumpTime ==0)
			{
				return;
			}
			var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
			var velocity = elapsed * bumpVelocity;
			if (bumpTime > 0)
			{
				bumpTime -= elapsed;
				bumpPosition += velocity;
			}else if (bumpPosition > 0)
			{
				bumpPosition -= velocity;
			}
			if (bumpPosition <= 0)
			{
				bumpPosition = 0;
				bumpTime = 0;
				this.State = State.Bumped;
			}
			this.Shift.Y = -bumpPosition;
		}
	}
}
